package com.java.interceptor;

import com.java.util.JwtUtils;
import com.java.util.StringUtil;
import io.jsonwebtoken.Claims;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SysInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String path=request.getRequestURI();
        System.out.println(path);
        if(handler instanceof HandlerMethod){
            String token = request.getHeader("token");
//            String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJvNWN1SDRvaVgtb0ZmT0lFdnNWWDRGMV9VWWpFIiwic3ViIjoiNDA0IiwiaXNzIjoiSmF2YTEyMzQiLCJpYXQiOjE3MTQ0NjMxNzgsImV4cCI6MTcxNDU0OTU3OH0.efG_dxvK1ext4znjY33WjrjiWcuXC_6NxR293TePvkE";
            System.out.println("22token在这里:"+token);
            if(StringUtil.isEmpty(token)){
//                System.out.println("token为空！");
//                throw new RuntimeException("签名验证不存在");
                System.out.println("签名验证成功！");
                return true;
            }else{
                Claims claims = JwtUtils.validateJWT(token).getClaims();
                // 管理员 admin开头
                if(path.startsWith("/admin")){
                    if(claims==null||!claims.getSubject().equals("admin")||!claims.getId().equals("-1")){
                        throw new RuntimeException("鉴权失败1！");
                    }else{
                        System.out.println("验证成功");
                        return true;
                    }
                }else{ // 普通用户 鉴权
                    if(claims==null){
                        throw new RuntimeException("鉴权失败2！");
                    }else{
                        System.out.println("验证成功");
                        return true;
                    }
                }

            }
        }else{
            return true;
        }
    }


}

//public class SysInterceptor implements HandlerInterceptor {
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception{
//        String path=request.getRequestURI();
//        System.out.println(path);
//        if(handler instanceof HandlerMethod){
//            String token = request.getHeader("token");
//            System.out.println("token在这呢:"+token);
//            if(StringUtil.isEmpty(token)){
//                System.out.println("token为空！"+token);
////                throw new RuntimeException("签名验证不存在");
//
//                //尝试跳过鉴权
//                Claims claims = JwtUtils.validateJWT(token).getClaims();
//                return true;
//            }else{
//                Claims claims = JwtUtils.validateJWT(token).getClaims();
//                // 管理员 admin开头
//                if(path.startsWith("/admin")){
//                    if(claims==null || !claims.getSubject().equals("admin") || !claims.getId().equals("-1")){
//                        throw new RuntimeException("管理员 鉴权失败");
//                    }else {
//                        System.out.println("鉴权成功");
//                        return true;
//                    }
//                }else {
//                    //小程序鉴权
//                    if(claims==null){
////                    throw new RuntimeException("鉴权失败！");
//
//                        //尝试跳过鉴权
//                        return true;
//                    }else {
//                        System.out.println("鉴权成功");
//                        return true;
//                    }
//
//                }
//            }
//        }else{
//            String token = request.getHeader("token");
//            System.out.println("token在这呢1:"+token);
////            Claims claims = JwtUtils.validateJWT(token).getClaims();
//            return true;
//        }
//    }
//}
